package com.hz.friends.chat;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.hz.friends.activity.BaseActivity;
import com.hz.friends.activity.LoginActivity;
import com.hz.friends.chat.Helper.CCPIntentUtils;
import com.hz.friends.chat.Helper.CCPUtil;
import com.hz.friends.global.Global;


public abstract class BaseChatActivity extends BaseActivity{
	
	private InternalReceiver internalReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		registerReceiver(new String[] { CCPIntentUtils.INTENT_KICKEDOFF,
				Intent.ACTION_MEDIA_MOUNTED, Intent.ACTION_MEDIA_REMOVED ,CCPIntentUtils.INTENT_IM_RECIVE});
	}

	
	
	protected final void registerReceiver(String[] actionArray) {
		if (actionArray == null) {
			return;
		}
		IntentFilter intentfilter = new IntentFilter(
				Global.INTETN_ACTION_EXIT_SOFT);
		intentfilter.addAction(CCPIntentUtils.INTENT_CONNECT_CCP);
		intentfilter.addAction(CCPIntentUtils.INTENT_DISCONNECT_CCP);
		for (String action : actionArray) {
			intentfilter.addAction(action);
		}
		if (internalReceiver == null) {
			internalReceiver = new InternalReceiver();
		}
		registerReceiver(internalReceiver, intentfilter);
	}

	class InternalReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent == null) {
				return;
			}

			if (CCPIntentUtils.INTENT_KICKEDOFF.equals(intent.getAction())
					|| CCPIntentUtils.INTENT_INVALIDPROXY.equals(intent
							.getAction())) {//断开连接

				String message = "您的账号在其他地方已经登录";
				if (CCPIntentUtils.INTENT_INVALIDPROXY.equals(intent
						.getAction())) {
					message = "无效的代理,与云通讯服务器断开";
				}
				Dialog dialog = new AlertDialog.Builder(getApplicationContext())
						.setTitle("提示")
						.setMessage(message)
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {
										dialog.dismiss();
										CCPUtil.exitCCPDemo();
										launchCCP();
									}

								}).create();
				dialog.show();

			} else if (intent != null
					&& Global.INTETN_ACTION_EXIT_SOFT.equals(intent.getAction())) {//退出应用程序
				CCPUtil.exitCCPDemo();
				finish();
			} else {
				if (intent == null || TextUtils.isEmpty(intent.getAction())) {
					return;
				}

				/**
				 * version 3.5 for listener SDcard status
				 */
				if (Intent.ACTION_MEDIA_REMOVED.equalsIgnoreCase(intent
						.getAction())
						|| Intent.ACTION_MEDIA_MOUNTED.equalsIgnoreCase(intent
								.getAction())) {

//					updateExternalStorageState();
					return;
				}

				onReceiveBroadcast(intent);
			}
		}
	}
	
	
	protected void onReceiveBroadcast(Intent intent) {
		
	}
	
	
	
	/**
	 * 
	 */
	public void launchCCP() {
		Intent intent = new Intent();   
		intent.setClass(this, LoginActivity.class);  
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
		startActivity(intent);
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(internalReceiver);
	}
}
